package com.research.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.research.common.CustomException;
import com.research.common.R;
import com.research.entity.Department;
import com.research.entity.Teacher;
import com.research.mapper.DepartmentMapper;
import com.research.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/*前端控制器*/
@RestController
@RequestMapping("/teacher")
public class TeacherController extends ServiceImpl<DepartmentMapper, Department> {
    @Autowired
    private TeacherService teacherService;

    //管理员登录
    @PostMapping("loginadmin")
    public R loginadmin(@RequestBody Teacher teacher) {
        return R.success(teacherService.loginadmin(teacher));

    }
    //用户登录
    @PostMapping("loginuser")
    public R loginuser(@RequestBody Teacher teacher) {
        return R.success(teacherService.loginuser(teacher));
    }
    //注册
    @PostMapping("reg")
    public R reg(@RequestBody Teacher teacher) {
        return R.success(teacherService.reg(teacher));
    }
    @GetMapping("list")
    public R list(String keyword) {
        return R.success(teacherService.list(
                new QueryWrapper<Teacher>().like(keyword != null, "teacher_name", keyword)
        ));
    }

    @PostMapping("save")
    public R save(@RequestBody Teacher teacher) {
        return R.success(teacherService.save(teacher));
    }

    @PostMapping("update")
    public R update(@RequestBody Teacher teacher) {
        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("department_id", teacher.getDepartmentId());
        if(baseMapper.selectOne(queryWrapper)!=null){
            return R.success(teacherService.updateById(teacher));
        }
        else{
            throw new CustomException("请填写正确部门id");
        }
    }
    @PostMapping("delete")
    public R delete(Integer id) {
        return R.success(teacherService.removeById(id));
    }

    @GetMapping("detail")
    public R detail(Integer id) {
        return R.success(teacherService.getById(id));
    }
}


